<template>
    <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="120px" v-show="showSearch">
        <el-form-item label="省" prop="provinceId">
            <el-select v-model="queryParams.provinceId" placeholder="请选择省" @change="chooseProvince" clearable>
                <el-option v-for="item in provinceData" :key="item.id" :label="item.name" :value="item.id">
                </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="市" prop="cityId">
            <el-select v-model="queryParams.cityId" placeholder="请选择市" @change="chooseCity" clearable>
                <el-option v-for="item in cityData" :key="item.id" :label="item.name" :value="item.id">
                </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="区/县" prop="countiesId">
            <el-select v-model="queryParams.countiesId" placeholder="请选择区/县" clearable>
                <el-option v-for="item in areaData" :key="item.id" :label="item.name" :value="item.id">
                </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="报告名称" prop="srName">
            <el-input v-model="queryParams.srName" placeholder="请输入报告名称" clearable />
        </el-form-item>
        <el-form-item label="报告类型" prop="srType">
            <el-select v-model="queryParams.srType" placeholder="请选择报告类型" clearable>
                <el-option v-for="item in optionType" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
            </el-select>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="handleQuery">搜索</el-button>
            <el-button @click="resetQuery">重置</el-button>
        </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
            <el-button type="primary" @click="handleDownloads" :disabled="multiple">
                <svg-icon class-name="download-icon" icon-class="download" /><span style="margin-left:10px">批量下载</span>
            </el-button>
        </el-col>
        <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dataList" stripe @selection-change="handleSelectionChange" row-key="id">
        <el-table-column type="selection" width="55" align="center" :reserve-selection="true" />
        <el-table-column label="报告ID" align="center" prop="srNumber" />
        <el-table-column label="报告名称" align="center" prop="srName" width="255" show-overflow-tooltip />
        <el-table-column label="提交人" align="center" prop="submiter" />
        <el-table-column label="报告类型" align="center" prop="srType" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template #default="scope">
                <el-button type="text" @click="handleDownload(scope.row)">报告下载</el-button>
                <el-button type="text" @click="handlePass(scope.row)">通过</el-button>
                <el-button type="text" @click="handleNoPass(scope.row)">不通过</el-button>
            </template>
        </el-table-column>
    </el-table>

    <!-- <pagination v-show="total > 0" :total="total" v-model:page="queryParams.page" v-model:limit="queryParams.rows"
        @pagination="getList" /> -->

    <!-- 审核不通过对话框 -->
    <el-dialog v-model="noPassOpen" title="审核不通过" width="900px" append-to-body>
        <p></p>
        <el-form ref="noPassRef" :model="form" :rules="rules" label-width="150px">
          <el-form-item label="审核结果"><span>不通过</span></el-form-item>
          <el-form-item label="不通过原因" prop="noPassresult">
            <el-input v-model="form.noPassresult" type="textarea" maxlength="500" show-word-limit
              :autosize="{ minRows: 5, maxRows: 5}" placeholder="请输入不通过原因" />
          </el-form-item>
      </el-form>
        <template #footer>
            <div class="dialog-footer">
                <el-button type="primary" @click="confirmNoPass">确 定</el-button>
                <el-button @click="noPassOpen=false">取 消</el-button>
            </div>
        </template>
    </el-dialog>
</template>
  
<script setup>
import { getProvince, getChildArea } from "@/api/system/division";
import { getAllList,pass } from "@/api/business/reportPending.js"
const { proxy } = getCurrentInstance();
const dataList = ref([]);
const ids = ref();
const loading = ref(false);
const single = ref(true);
const multiple = ref(true);
const noPassOpen = ref(false);
const total = ref(0);
const showSearch = ref(true);
const srId = ref();
const provinceData = ref([]); //省
const cityData = ref([]); //市
const areaData = ref([]); //区、县
const optionType = [{ label: '地震灾害风险评估报告', value: '地震灾害风险评估报告' }, { label: '辅助对策报告', value: '辅助对策报告' }]

const data = reactive({
    queryParams: {
        // page: 1,
        // rows: 10,
        shStatus:0,
        provinceId: undefined,
        cityId: undefined,
        countiesId: undefined,
        srName: undefined,
        srType: undefined,
    },
    form:{
        noPassresult:undefined,
    },
    rules:{
        noPassresult: [{ required: true, message: "不通过原因不能为空", trigger: "blur" }],
    }
})
const { queryParams, form, rules } = toRefs(data)


/** 搜索按钮操作 */
function handleQuery() {
    // queryParams.value.page = 1;
    getList();
}

/** 重置按钮操作 */
function resetQuery() {
    dataList.value = [];
    proxy.resetForm("queryRef");
    handleQuery();
}

/** 查询数据列表 */
function getList() {
    loading.value = true;
    getAllList(queryParams.value).then(response => {
        // dataList.value = response.data.actReports ? response.data.actReports : [];
        // total.value = response.data.total;
        dataList.value = response.data?response.data:[];
        loading.value = false;
    });
}

// 多选框选中数据
function handleSelectionChange(selection) {
    ids.value = selection.map(item => item.attachId);
    single.value = selection.length != 1;
    multiple.value = !selection.length;
}

/** 批量下载操作 */
function handleDownloads() {
    console.log(ids.value);
    proxy.download("/attachment/downForZip/"+ ids.value.join(","), { }, '报告.zip');
};

// 单个下载文件
function handleDownload(row) {
    var query = {
        attachId: row.attachId,
        name: row.srName
    }
    location.href = import.meta.env.VITE_APP_BASE_API + "/attachment/downLoadFile?attachId=" + encodeURIComponent(query.attachId);
}

// 通过
function handlePass(row) {
    pass({srId:row.srId,shStatus:'2'}).then(res=>{
        proxy.$modal.msgSuccess("已通过");
        getList();
    })
}

// 确定不通过
function confirmNoPass(){
    proxy.$refs["noPassRef"].validate(valid => {
    if (valid) {
        pass({reason:form.value.noPassresult,srId:srId.value,shStatus:'3'}).then(res=>{
            proxy.$modal.msgSuccess("不通过");
            noPassOpen.value = false;
            getList();
        })
    }
  });
}

// 不通过
function handleNoPass(row) {
    noPassOpen.value = true;
    proxy.resetForm("noPassRef");
    srId.value = row.srId;
}

/** 获取省列表 */
function getProvinceData() {
    getProvince().then((response) => {
        provinceData.value = response.data;
    });
}

function chooseProvince(value) {
    queryParams.value.cityId = null;
    queryParams.value.countiesId = null;
    cityData.value = [];
    areaData.value = [];
    getChildArea(value).then(response => {
        cityData.value = response.data;
    });
}

function chooseCity(value) {
    queryParams.value.countiesId = null;
    areaData.value = [];
    getChildArea(value).then((response) => {
        areaData.value = response.data;
    });
}


getProvinceData();
getList();
</script>
  
<style lang="scss" scoped>
.hsdTime {
    margin-right: 0px !important;

}
</style>